package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;

import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.OceanPreStoragePositionBiz;
import com.egao.orderB.entity.OceanPreStoragePosition;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-10-19 16:47:57
 * @version ：1.0
 */
@Repository
public class OceanPreStoragePositionBizImpl extends BaseDao implements OceanPreStoragePositionBiz {

	@Override
	public JsonResult list(PageResult2<T> page, OceanPreStoragePosition oceanPreStoragePosition, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanPreStoragePosition> rm = new BeanPropertyRowMapper<OceanPreStoragePosition>(OceanPreStoragePosition.class);
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.booking_agent_id,o.booking_agent");
    sql.append(",o.vessel,o.voyage,o.air_route,o.go_ship_day,o.bill_of_lading");
    sql.append(",o.ship,o.box_type,o.price,o.deposit,o.deposit_path,o.create_time");
		sql.append(",o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_pre_storage_position o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where 1=1 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
		if (Utils.isNotEmpty(oceanPreStoragePosition.getDate1())) {
			sql.append(" and o.go_ship_day>= :date1 ");
			map.put("date1", oceanPreStoragePosition.getDate1());
		}
		if (Utils.isNotEmpty(oceanPreStoragePosition.getDate2())) {
			sql.append(" and o.go_ship_day<=:date2 ");
			map.put("date2", oceanPreStoragePosition.getDate2());
		}
		if (Utils.isNotEmpty(oceanPreStoragePosition.getBillOfLading())) {
			sql.append(" and instr(o.bill_of_lading,:bill_of_lading)>0 ");
			map.put("bill_of_lading", oceanPreStoragePosition.getBillOfLading());
		}
		if(Utils.isNotEmpty(oceanPreStoragePosition.getDeleted()) && oceanPreStoragePosition.getDeleted()==2){
			sql.append(" and o.deleted=2 ");
		} else {
			sql.append(" and o.deleted=0 ");
		}
		if(Utils.isNotEmpty(oceanPreStoragePosition.getSort())&&Utils.isNotEmpty(oceanPreStoragePosition.getOrder())){
			sql.append(" order by o."+oceanPreStoragePosition.getSort()+" "+oceanPreStoragePosition.getOrder());
		} else {
			sql.append(" order by o.create_time desc");
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<OceanPreStoragePosition> list(OceanPreStoragePosition oceanPreStoragePosition, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanPreStoragePosition> rm = new BeanPropertyRowMapper<OceanPreStoragePosition>(OceanPreStoragePosition.class);
    StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.booking_agent_id,o.booking_agent");
		sql.append(",o.vessel,o.voyage,o.air_route,o.go_ship_day,o.bill_of_lading");
    sql.append(",o.ship,o.box_type,o.price,o.deposit,o.deposit_path,o.create_time");
		sql.append(",o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_pre_storage_position o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and o.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(oceanPreStoragePosition)){
			if (Utils.isNotEmpty(oceanPreStoragePosition.getDate1())) {
				sql.append(" and o.go_ship_day>= :date1 ");
				map.put("date1", oceanPreStoragePosition.getDate1());
			}
			if (Utils.isNotEmpty(oceanPreStoragePosition.getDate2())) {
				sql.append(" and o.go_ship_day<=:date2 ");
				map.put("date2", oceanPreStoragePosition.getDate2());
			}
			if (Utils.isNotEmpty(oceanPreStoragePosition.getBillOfLading())) {
				sql.append(" and instr(o.bill_of_lading,:bill_of_lading)>0 ");
				map.put("bill_of_lading", oceanPreStoragePosition.getBillOfLading());
			}
      if(Utils.isNotEmpty(oceanPreStoragePosition.getSort())&&Utils.isNotEmpty(oceanPreStoragePosition.getOrder())){
        sql.append(" order by o."+oceanPreStoragePosition.getSort()+" "+oceanPreStoragePosition.getOrder());
      } else {
        sql.append(" order by o.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public List<OceanPreStoragePosition> boxTypeNum(OceanPreStoragePosition oceanPreStoragePosition, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanPreStoragePosition> rm = new BeanPropertyRowMapper<OceanPreStoragePosition>(OceanPreStoragePosition.class);
    StringBuffer sql = new StringBuffer("select ");
    sql.append(" o.box_type,count(1) price ");
		sql.append(" from chf_ocean_pre_storage_position o ");
		sql.append(" where o.deleted=0 ");
    sql.append(" group by box_type ");
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public OceanPreStoragePosition getById(OceanPreStoragePosition oceanPreStoragePosition) {
		RowMapper<OceanPreStoragePosition> rm = new BeanPropertyRowMapper<OceanPreStoragePosition>(OceanPreStoragePosition.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.booking_agent_id,o.booking_agent");
		sql.append(",o.vessel,o.voyage,o.air_route,o.go_ship_day,o.bill_of_lading");
    sql.append(",o.ship,o.box_type,o.price,o.deposit,o.deposit_path,o.create_time");
		sql.append(",o.create_user,o.update_time,o.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_ocean_pre_storage_position o ");
    sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
    sql.append(" where o.deleted=0 and o.id=:id ");
		map.put("id", oceanPreStoragePosition.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(OceanPreStoragePosition oceanPreStoragePosition, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_ocean_pre_storage_position (");
    sql.append("organization_id,booking_agent_id,booking_agent,ship,box_type");
		sql.append(",vessel,voyage,air_route,go_ship_day,bill_of_lading");
		sql.append(",price,deposit,deposit_path,create_user,create_time) values ( ");
    sql.append(":organization_id,:booking_agent_id,:booking_agent,:ship,:box_type");
		sql.append(",:vessel,:voyage,:air_route,:go_ship_day,:bill_of_lading");
		sql.append(",:price,:deposit,:deposit_path,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("booking_agent_id", oceanPreStoragePosition.getBookingAgentId()==null?null:oceanPreStoragePosition.getBookingAgentId()+"");
    map.put("booking_agent", oceanPreStoragePosition.getBookingAgent());
    map.put("ship", oceanPreStoragePosition.getShip());
		map.put("vessel", oceanPreStoragePosition.getVessel());
		map.put("voyage", oceanPreStoragePosition.getVoyage());
		map.put("air_route", oceanPreStoragePosition.getAirRoute());
		map.put("go_ship_day", oceanPreStoragePosition.getGoShipDay());
		map.put("bill_of_lading", oceanPreStoragePosition.getBillOfLading());
    map.put("box_type", oceanPreStoragePosition.getBoxType());
    map.put("price", oceanPreStoragePosition.getPrice());
    map.put("deposit", oceanPreStoragePosition.getDeposit());
    map.put("deposit_path", oceanPreStoragePosition.getDepositPath());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(OceanPreStoragePosition oceanPreStoragePosition, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_ocean_pre_storage_position set ");
    sql.append("booking_agent_id=:booking_agent_id,booking_agent=:booking_agent,ship=:ship,box_type=:box_type ");
		sql.append(",vessel=:vessel,voyage=:voyage,air_route=:air_route,go_ship_day=:go_ship_day");
		sql.append(",bill_of_lading=:bill_of_lading");
		sql.append(",price=:price,deposit=:deposit,deposit_path=:deposit_path,update_time=now(),update_user=:update_user where id=:id ");
    map.put("booking_agent_id", oceanPreStoragePosition.getBookingAgentId()==null?null:oceanPreStoragePosition.getBookingAgentId()+"");
		map.put("booking_agent", oceanPreStoragePosition.getBookingAgent());
    map.put("ship", oceanPreStoragePosition.getShip());
    map.put("vessel", oceanPreStoragePosition.getVessel());
    map.put("voyage", oceanPreStoragePosition.getVoyage());
    map.put("air_route", oceanPreStoragePosition.getAirRoute());
    map.put("go_ship_day", oceanPreStoragePosition.getGoShipDay());
    map.put("bill_of_lading", oceanPreStoragePosition.getBillOfLading());
    map.put("box_type", oceanPreStoragePosition.getBoxType());
    map.put("price", oceanPreStoragePosition.getPrice());
		map.put("deposit", oceanPreStoragePosition.getDeposit());
		map.put("deposit_path", oceanPreStoragePosition.getDepositPath());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", oceanPreStoragePosition.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean cancel(OceanPreStoragePosition oceanPreStoragePosition, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_ocean_pre_storage_position set deleted=2" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", oceanPreStoragePosition.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(),map);
		return count > 0;
	}

	@Override
	public boolean remove(OceanPreStoragePosition oceanPreStoragePosition, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(oceanPreStoragePosition.getId().length()>0){
			for(String ID : oceanPreStoragePosition.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_ocean_pre_storage_position set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
